﻿using System;
using System.Data;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.ClientTasks.Attributes;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using CdcSoftware.Pivotal.Engine.UI.DataTypes;
using CdcSoftware.Pivotal.Engine.Types.Database;
using CdcSoftware.Pivotal.Engine.Types.DataTemplates;
using CdcSoftware.Pivotal.Engine.Types.Localization;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine.UI.DataTypes.FormParams;
using System.Windows.Forms;

namespace Pcm.Retail.Client.NotificationHandlers
{
    public partial class InventoryTransferNotificationHandler
    {
        /// <summary>
        /// Handles OnFormIdRequested notification on RE_Inventory_Transfer Table to set the form name for a given Record Id.
        /// </summary>
        /// <param name="recordId"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        /// <history>
        /// Revision#      Date            Author          Description
        /// 6.0.9.0        29/12/2011      VT              Initial version for Inventory.
        /// </history>
        [NotificationEvent("OnFormIdRequested", Table = "RE_Inventory_Transfer")]
        public FormIdRequestReturnValue OnInventoryTransferFormIdRequested(Id recordId, Table table)
        {
            try
            {   
                FormIdRequestReturnValue retVal = new FormIdRequestReturnValue();
                
                IDataAccessService dataAcess = ClientContext.ClientAPIService.GetService<IDataAccessService>();
                ILanguageDictionaryService ldService = ClientContext.ClientAPIService.GetService<ILanguageDictionaryService>();

                string formName = string.Empty;
                if (recordId == null)
                {
                     DataTable inventoryTransfer = Globals.GetDataTable("RE_Inventory Transfer for Inventory Transfer Id?", new object[] { recordId },
                        "Transfer_Type", "From_Storage_Location_Id", "To_Storage_Location_Id");
                    if (String.Equals(TypeConvert.ToString(inventoryTransfer.Rows[0]["Transfer_Type"]), "RMA"))
                    {
                        formName = "RE_RMA";
                    }
                    else
                    {
                        formName = "RE_Goods_Issue";
                    }
                }
                else
                {
                    if ((Globals.CurrentUserInSecurityGroup("Retail POS")))
                    {
                        DataTable inventoryTransfer = Globals.GetDataTable("RE_Inventory Transfer for Inventory Transfer Id?", new object[] { recordId },
                            "Transfer_Type", "From_Storage_Location_Id", "To_Storage_Location_Id");
                        if (String.Equals(TypeConvert.ToString(inventoryTransfer.Rows[0]["Transfer_Type"]), "RMA"))
                        {
                            formName = "RE_RMA";
                        }
                        if (String.Equals(TypeConvert.ToString(inventoryTransfer.Rows[0]["Transfer_Type"]), "PO Receive") ||
                            String.Equals(TypeConvert.ToString(inventoryTransfer.Rows[0]["Transfer_Type"]), "Inventory Receive"))
                        {
                            formName = "RE_Goods_Receive";
                        }
                        if (String.Equals(TypeConvert.ToString(inventoryTransfer.Rows[0]["Transfer_Type"]), "Inventory Transfer"))
                        {
                            Id currEmpStore = Id.Create(Globals.SqlIndex("Employee", "RE_Store_Id", Globals.SystemClient.UserProfile.EmployeeId));
                            Id currEmpstorageLocationId = null;
                            DataTable storageLoc = Globals.GetDataTable("RE Storage Location For Primary Locations ?",
                                                        new object[] { currEmpStore.ToByteArray() }, new string[] { "RE_Storage_Location_Id" });
                            if (storageLoc != null && storageLoc.Rows.Count > 0)
                                currEmpstorageLocationId = Id.Create(storageLoc.Rows[0]["RE_Storage_Location_Id"]);
                            if (currEmpstorageLocationId != null)
                            {
                                if (currEmpstorageLocationId.Equals(Id.Create(inventoryTransfer.Rows[0]["From_Storage_Location_Id"])))
                                    formName = "RE_Goods_Issue";
                                else if (currEmpstorageLocationId.Equals(Id.Create(inventoryTransfer.Rows[0]["To_Storage_Location_Id"])))
                                    formName = "RE_Goods_Receive";
                            }
                            else
                                formName = "RE_Goods_Issue";
                        }
                    }
                    else
                    {
                        
                        DataTable inventoryTransfer = Globals.GetDataTable("RE_Inventory Transfer for Inventory Transfer Id?", new object[] { recordId },
                            "Status","Transfer_Type", "From_Storage_Location_Id", "To_Storage_Location_Id");
                        
                        if ((Globals.CurrentUserInSecurityGroup("Retail Administrator") || Globals.CurrentUserInSecurityGroup("Retail Operations"))
                            && String.Equals(TypeConvert.ToString(inventoryTransfer.Rows[0]["Transfer_Type"]), "Inventory Transfer"))
                        {
                            if (String.Equals(TypeConvert.ToString(inventoryTransfer.Rows[0]["Transfer_Type"]), "RMA"))
                            {
                                formName = "RE_RMA";
                            }
                            string status = TypeConvert.ToString(inventoryTransfer.Rows[0]["Status"]);
                            if (String.Equals(status, Status.Received) || String.Equals(status, Status.Cancel))
                                formName = "RE_Goods_Receive";
                            else if (String.Equals(status, Status.New) || String.Equals(status, Status.Hold))
                                formName = "RE_Goods_Issue";
                            else if (String.Equals(status, Status.Pending) || String.Equals(status, Status.Issued))
                            {
                                int response = Globals.DialogRadio("", "Please select the Form:", "Goods Issue", "Goods Receive");
                                if (response == 0)
                                    formName = "RE_Goods_Issue";
                                else if (response == 1)
                                    formName = "RE_Goods_Receive";
                            }
                        }
                        else
                            formName = "RE_Goods_Issue";
                    }
                }
                /*
                if (recordId == null)
                {
                    formName = "RE_Goods_Issue";
                }
                else
                {
                    DataTable inventoryTransfer = Globals.GetDataTable("RE_Inventory Transfer for Inventory Transfer Id?", new object[] { recordId },
                        "From_Storage_Location_Id", "To_Storage_Location_Id", "Temp_Goods_Receive", "Status");
                    if (inventoryTransfer != null && inventoryTransfer.Rows.Count >= 0)
                    {
                        if (TypeConvert.ToBoolean(inventoryTransfer.Rows[0]["Temp_Goods_Receive"]))
                            formName = "RE_New_Goods_Receive(Temp)";
                        else
                        {
                            if (Globals.CurrentUserInSecurityGroup("Retail POS"))
                            {
                                Id currEmpStore = Id.Create(Globals.SqlIndex("Employee", "RE_Store_Id", Globals.SystemClient.UserProfile.EmployeeId));
                                Id currEmpstorageLocationId = null;
                                DataTable storageLoc = Globals.GetDataTable("RE Storage Location For Primary Locations ?",
                                                            new object[] { currEmpStore.ToByteArray() }, new string[] { "RE_Storage_Location_Id" });
                                if (storageLoc != null && storageLoc.Rows.Count > 0)
                                    currEmpstorageLocationId = Id.Create(storageLoc.Rows[0]["RE_Storage_Location_Id"]);

                                if (currEmpstorageLocationId.Equals(Id.Create(inventoryTransfer.Rows[0]["From_Storage_Location_Id"])))
                                    formName = "RE_Goods_Issue";
                                else if (currEmpstorageLocationId.Equals(Id.Create(inventoryTransfer.Rows[0]["To_Storage_Location_Id"])))
                                    formName = "RE_Goods_Receive";
                                else
                                    formName = "RE_Goods_Issue";
                            }
                            //DS:10012013 - Workflow change for Admin/Operations user.
                            else if (Globals.CurrentUserInSecurityGroup("Retail Administrator") || Globals.CurrentUserInSecurityGroup("Retail Operations"))
                            {
                                string status = TypeConvert.ToString(inventoryTransfer.Rows[0]["Status"]);
                                if (String.Equals(status, Status.Received) || String.Equals(status, Status.Cancel))
                                    formName = "RE_Goods_Receive";
                                else if (String.Equals(status, Status.New) || String.Equals(status, Status.Hold))
                                    formName = "RE_Goods_Issue";
                                else if (String.Equals(status, Status.Pending) || String.Equals(status, Status.Issued))
                                {
                                    int response = Globals.DialogRadio("", "Please select the Form:", "Goods Issue", "Goods Receive");
                                    if (response == 0)
                                        formName = "RE_Goods_Issue";
                                    else if (response == 1)
                                        formName = "RE_Goods_Receive";
                                }
                            }
                            else
                                formName = "RE_Goods_Issue";
                        }
                    }
                }
                */
                if (!String.Equals(formName, string.Empty))
                    retVal.FormId = dataAcess.SystemClient.UserProfile.GetMetaItem<CdcSoftware.Pivotal.Engine.Client.Types.Form>(formName).Id;
                else
                    retVal.FormId = null;

                return retVal;
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
                return null;
            }
        }

        /// <summary>
        /// Status Struct.
        /// </summary>
        private struct Status
        {
            //public const string FieldName = "Status";
            public const string Issued = "Issued";
            public const string Cancel = "Cancel";
            public const string New = "New";
            public const string Hold = "Hold";
            public const string Pending = "Pending";
            public const string Received = "Received";
        }
    }
}